/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.jmmarca.dao.generic;

import java.util.List;

import org.droidpersistence.dao.DroidDao;
import org.droidpersistence.dao.TableDefinition;

/**
 *
 * @author Jean Michel
 */
public class AbstractDAO<T> extends DroidDao<T, Integer> {

    private Class<T> modelClass = null;
    private TableDefinition<T> tableDefinition = null;
    private DataManager dataManager = null;

    public int salvar(AbstractEntity objeto) {
    	if(objeto.getId() == null){
    		return inserir(objeto);
    	} else {
    		return atualizar(objeto);
    	}
    }

    public int inserir(AbstractEntity objeto) {
        long result = 0;
        try {
            this.getDataManager().getDatabase().beginTransaction();
            objeto.setId(null);
            result = this.save((T) objeto);
            this.getDataManager().getDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.getDataManager().getDatabase().endTransaction();
        return (int) result;
    }

    public int atualizar(AbstractEntity objeto) {
        long result = 0;
        try {
            this.getDataManager().getDatabase().beginTransaction();
            this.update((T) objeto, objeto.getId());
            this.getDataManager().getDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.getDataManager().getDatabase().endTransaction();
        return (int) result;
    }

    public boolean remover(Integer id) {
        boolean result = false;
        try {
            this.getDataManager().getDatabase().beginTransaction();
            super.delete(id);
            this.getDataManager().getDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.getDataManager().getDatabase().endTransaction();

        return result;
    }

    public boolean remover(AbstractEntity classe) {
        boolean result = false;
        try {
            this.getDataManager().getDatabase().beginTransaction();
            super.delete(classe.getId());
            this.getDataManager().getDatabase().setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.getDataManager().getDatabase().endTransaction();

        return result;
    }

    public T encontrar(int id) {
        T object = null;
        try {
            object = this.get(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    public List<T> listar() {
        return super.getAll();
    }
    /**
    *
    * "ColumnName LIKE ?" ,new String[]{_data+"%"}
    *
    * @param clauseWhere
    * @param clauseValues
    * @return
    */
   public List<T> listarPor(String clauseWhere, String[] clauseValues) {
       return super.getAllbyClause(clauseWhere, clauseValues, null, null, null);
   }
   
    public List<T> listarPor(String clause, String[] clauseArgs, String groupBy, String having, String orderBy) {
        return super.getAllbyClause(clause, clauseArgs, groupBy, having, orderBy);
    }

    public List<T> listarPor(String clause) {
        return super.getAllbyClause(clause, null, null, null, null);
    }

    public AbstractDAO(Class<T> model, TableDefinition<T> tableDefinition, DataManager dataManager) {
        super(model, tableDefinition, dataManager.getDatabase());
        this.dataManager = dataManager;
        this.modelClass = model;
        this.tableDefinition = tableDefinition;
    }

    private DataManager getDataManager() {
        return dataManager;
    }

    private void setDataManager(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    private Class<T> getModelClass() {
        return modelClass;
    }

    private void setModelClass(Class<T> modelClass) {
        this.modelClass = modelClass;
    }
}
